/*
 * fbank.com Inc.
 * Copyright (c) 2020 All Rights Reserved.
 * create by fm
 * date:2020-04-26
 */
package com.ouwen.smartpay.service.impl;

import com.fm.common.emuns.TaskExecuteStatus;
import com.fm.common.emuns.TaskStatus;
import com.fm.common.order.NotifyOrder;
import com.fm.core.common.service.EntityServiceImpl;
import com.fm.core.utils.Strings;
import com.fm.core.utils.mapper.BeanCopier;
import com.ouwen.smartpay.dao.NotifyMessageDao;
import com.ouwen.smartpay.entity.NotifyDetail;
import com.ouwen.smartpay.entity.NotifyMessage;
import com.ouwen.smartpay.entity.OrderInfo;
import com.ouwen.smartpay.openapi.order.NotifyOrderItem;
import com.ouwen.smartpay.service.NotifyDetailService;
import com.ouwen.smartpay.service.NotifyMessageService;
import com.ouwen.smartpay.service.OrderInfoService;
import com.ouwen.smartpay.service.notify.NotifyConfig;
import com.ouwen.smartpay.utils.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 异步通知消息表 Service实现
 * <p>
 * Date: 2020-04-26 10:57:42
 *
 * @author fm
 */
@Service("notifyMessageService")
public class NotifyMessageServiceImpl extends EntityServiceImpl<NotifyMessage, NotifyMessageDao> implements NotifyMessageService {

    @Resource
    private NotifyMessageDao notifyMessageDao;

    @Resource
    private OrderInfoService orderInfoService;

    @Resource
    private NotifyDetailService notifyDetailService;

    @Override
    public NotifyMessage findByPartnerIdAndSubPartnerIdAndMerchOrderNo(String partnerId, String subPartnerId, String merchOrderNo) {
        return notifyMessageDao.findByPartnerIdAndSubPartnerIdAndMerchOrderNo(partnerId,subPartnerId, merchOrderNo);
    }

    @Override
    public int updateProccessingStatus(Long id) {
        return notifyMessageDao.updateProccessingStatus(id);
    }

    @Override
    public List<NotifyMessage> listUnProcessed(int topNum) {
        return notifyMessageDao.listUnProcessed(topNum);
    }

    /***
     *
     * 通知流水入库，更新订单流水信息
     *
     * @author liyong
     * @date 11:43 2020-04-27
     * @param order
     * @param orderInfo
     * @exception
     * @return void
     **/
    @Transactional
    @Override
    public NotifyMessage saveNotifyMessageAndUpdateOrderInfo(NotifyOrder order, OrderInfo orderInfo) throws Exception {

        //构建NotifyMessage
        NotifyMessage message = buildNotifyMessage(orderInfo);

        //保存异步通知消息
        save(message);

        //保存异步通知内容
        saveNotifyDetail(order, message);

        //更新订单信息
        updateOrderInfo(orderInfo, order);

        return message;
    }

    private NotifyMessage buildNotifyMessage(OrderInfo orderInfo) {

        NotifyMessage message = new NotifyMessage();

        message.setGid(orderInfo.getGid());

        message.setExecuteStatus(TaskExecuteStatus.Unprocessed);

        message.setMerchOrderNo(orderInfo.getMerchOrderNo());

        //异步通信类型
        message.setMessageType(orderInfo.getMessageType());

        message.setPartnerId(orderInfo.getPartnerId());

        message.setSubPartnerId(orderInfo.getSubPartnerId());

        message.setRequestNo(orderInfo.getRequestNo());

        //初始化值
        message.setSendCount(0);

        message.setServiceName(orderInfo.getServiceName());

        message.setServiceGroup(orderInfo.getServiceGroup());

        message.setServiceVersion(orderInfo.getServiceVersion());

        message.setNotifyUrl(orderInfo.getNotifyUrl());

        message.setMaxSendCount(NotifyConfig.notifyCount);

        message.setStatus(TaskStatus.Waitting);

        return message;
    }

    /***
     *
     * 更新OrderInfo
     *
     * @author liyong
     * @date 5:15 PM 2020/5/6
     * @param orderInfo
     * @param order
     * @exception
     * @return void
     **/
    public void updateOrderInfo(OrderInfo orderInfo, NotifyOrder order) {

        orderInfo.setRespCode(order.getRespCode());

        orderInfo.setRespMsg(order.getRespMsg());

        if (Strings.isNoneBlank(order.getDescription()) && order.getDescription().length() > 1024) {

            orderInfo.setDescription(order.getDescription().substring(0, 1024));

        } else {

            orderInfo.setDescription(order.getDescription());

        }

        orderInfo.setTranStatus(order.getTranStatus());

        orderInfo.setTranDesc(order.getTranDesc());

        orderInfoService.update(orderInfo);
    }

    /***
     *
     * 保存异步通知详细内容
     *
     * @author liyong
     * @date 5:13 PM 2020/5/6
     * @param order
     * @param message
     * @exception
     * @return void
     **/
    public void saveNotifyDetail(NotifyOrder order, NotifyMessage message) throws Exception {

        NotifyOrderItem notifyOrderItem = new NotifyOrderItem();

        BeanCopier.copy(order, notifyOrderItem);

        //通知内容持久化
        String cotent = BeanUtil.mapToJson(BeanUtil.transBean2Map(notifyOrderItem));

        NotifyDetail notifyDetail = new NotifyDetail();

        notifyDetail.setNotifyId(message.getId());

        NotifyDetail notifyDetailDb = notifyDetailService.findOrderDetailNotifyInfoByOrderId(message.getId());

        if (notifyDetailDb != null) {

            notifyDetailDb.setNotifyInfo(cotent);

            notifyDetailService.update(notifyDetailDb);

        } else {

            notifyDetail.setNotifyInfo(cotent);

            notifyDetailService.save(notifyDetail);

        }

    }

}
